﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoolERP_Entities;

namespace CoolERP_DAL
{
    public interface IYSKZDDAL : IDAL<CW_YSKZD>
    {
        List<YSKZDModel> GetYSKZD(Dictionary<string, string> dc, string orderExpr, int? pageIndex, int pageSize, out int totalCount);
        List<vXSHT> GetXSHT(string orderExpr, int? pageIndex, int pageSize, out int totalCount);
}
    public class CW_YSKZDDAL : BaseDAL<CW_YSKZD>, IYSKZDDAL
    {
        public CW_YSKZDDAL(IUnitOfWork context)
            : base(context)
        { }
        public List<YSKZDModel> GetYSKZD(Dictionary<string, string> dc, string orderExpr, int? pageIndex, int pageSize, out int totalCount)
        {
            var result = from yskzd in dataContext.CW_YSKZD
                         join xsht in dataContext.XSHT on yskzd.XSHTID equals xsht.ID
                         join kh in dataContext.ZL_KH on xsht.KHID equals kh.ID
                         join users  in dataContext.Users on yskzd.CZRYID equals users.Id
                         join fplx in dataContext.CS_FPLX on yskzd.FPLXBH equals fplx.BH
                         select new YSKZDModel
                         {
                             ID = yskzd.ID,
                             BH = yskzd.BH,
                             XSHTID = yskzd.XSHTID,
                             XSHTBH = xsht.BH,
                             KHID = xsht.KHID,
                             KHMC = kh.MC,
                             KHJC = kh.JIANCHENG,
                             CZRYID=yskzd.CZRYID,
                             CZRYName=users.Name,
                             CHSJ = xsht.CHSJ,
                             DHSJ = xsht.DHSJ,
                             CJSJ = yskzd.CJSJ,
                             CKSJ = yskzd.CKSJ,
                             YSZE = yskzd.YSZE,
                             YISHOUZE=yskzd.YISHOUZE,
                             ZDZT = yskzd.ZDZT,
                             FPLXBH=fplx.BH,
                             FPLXMC=fplx.MC
                         };
            if (dc != null && dc.Count > 0)
            {
                if (dc.ContainsKey("ID") && !string.IsNullOrEmpty(dc["ID"]))
                {
                    Guid temp = Guid.Parse(dc["ID"]);
                    result = result.Where(t => t.ID == temp);
                }
                if (dc.ContainsKey("BH") && !string.IsNullOrEmpty(dc["BH"]))
                {
                    string temp = dc["BH"];
                    result = result.Where(t => t.BH == temp);
                }
                if (dc.ContainsKey("ZDZT") && !string.IsNullOrEmpty(dc["ZDZT"]))
                {
                    int temp = Convert.ToInt32(dc["ZDZT"]);
                    result = result.Where(t => t.ZDZT == temp);
                }

                if (dc.ContainsKey("KHJC") && !string.IsNullOrEmpty(dc["KHJC"]))
                {
                    string temp = dc["KHJC"];
                    result = result.Where(t => t.KHJC == temp);
                }
                if (dc.ContainsKey("CZRYName") && !string.IsNullOrEmpty(dc["CZRYName"]))
                {
                    string temp = dc["CZRYName"];
                    result = result.Where(t => t.CZRYName == temp);
                }
                if (dc.ContainsKey("KHID") && !string.IsNullOrEmpty(dc["KHID"]))
                {
                    Guid KHID = Guid.Parse(dc["KHID"]);
                    List<Guid> ids = dataContext.XSHT.Where(t => t.KHID == KHID).Select(t => t.ID).ToList();
                    result = result.Where(t => ids.Contains(t.XSHTID) );
                }

                if (dc.Keys.Contains("CJSJ") && !string.IsNullOrEmpty(dc["CJSJ"]))
                {
                    DateTime keyVal = DateTime.ParseExact(Convert.ToDateTime(dc["CJSJ"]).ToString("yyyyMMdd"), "yyyyMMdd", System.Threading.Thread.CurrentThread.CurrentCulture);
                    result = result.Where(t => t.CJSJ == keyVal);
                }
                if (dc.Keys.Contains("CKSJ") && !string.IsNullOrEmpty(dc["CKSJ"]))
                {
                    DateTime keyVal = DateTime.ParseExact(Convert.ToDateTime(dc["CKSJ"]).ToString("yyyyMMdd"), "yyyyMMdd", System.Threading.Thread.CurrentThread.CurrentCulture);
                    result = result.Where(t => t.CKSJ == keyVal);
                }
                if (dc.ContainsKey("YSZEMin") && !string.IsNullOrEmpty(dc["YSZEMin"]))
                {
                    double temp =double.Parse( dc["YSZEMin"]);
                    result = result.Where(t => t.YSZE >= temp);
                }
                if (dc.ContainsKey("YSZEMax") && !string.IsNullOrEmpty(dc["YSZEMax"]))
                {
                    double temp = double.Parse(dc["YSZEMax"]);
                    result = result.Where(t => t.YSZE <= temp);
                }
            }
            totalCount = result.Count();
            if (!string.IsNullOrEmpty(orderExpr))
                result = SortingExtensions.OrderUsingSortExpression(result, orderExpr);
            if (pageIndex == null)
                return result.ToList();
            else
                return result.Skip(Convert.ToInt32(--pageIndex) * pageSize).Take(pageSize).ToList();
        }

        public List<vXSHT> GetXSHT(string orderExpr, int? pageIndex, int pageSize, out int totalCount)
        {
            var result = from ht in dataContext.vXSHT
                         where !dataContext.CW_YSKZD.Select(t => t.XSHTID).Contains(ht.ID)
                         select ht;
            totalCount = result.Count();
            if (!string.IsNullOrEmpty(orderExpr))
                result = SortingExtensions.OrderUsingSortExpression(result, orderExpr);
            if (pageIndex == null)
                return result.ToList();
            else
                return result.Skip(Convert.ToInt32(--pageIndex) * pageSize).Take(pageSize).ToList();
        }
    }
}
